
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.faimg.pomar.modelo.dao;

import br.faimg.pomar.modelo.interfaces.CRUDInterface;
import br.faimg.pomar.modelo.pojo.NumeroSerie;
import br.faimg.pomar.modelo.pojo.OrdemProducao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author juliana
 */
public class NumeroSerieDAO implements CRUDInterface<NumeroSerie>{

    public void create(NumeroSerie pojo) {
     Connection con = null;
         PreparedStatement stm = null;
         String sql = "insert into NUM_SERIE values(?,?);";
        try {
             con = ConnectionManager.getInstance().getConnection();
             stm = con.prepareStatement(sql);
             stm.setString(1, pojo.getNumSerie());
             stm.setInt(2, pojo.getNumOp().getNumOrp());
             stm.execute();
        } catch (SQLException ex) {
            ex.printStackTrace();
          }finally {
            if (stm != null){
              try {
                stm.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            if (con != null){
              try {
                con.close();
              } catch (SQLException ex) {
                   ex.printStackTrace();
              }
            }
          }
    }
  
    public void update(NumeroSerie pojo) {
         throw new UnsupportedOperationException("Este cadastro não pode ser alterado!");
    }

    public List<NumeroSerie> readByExample(NumeroSerie pojo) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        String sql = null;
        boolean allResults = false;
        
        List<NumeroSerie> numeros = new ArrayList<NumeroSerie>();
 
        if (pojo == null) {
            pojo = new NumeroSerie();
        }
        
        if(pojo.getNumOp()!=null){
            if (pojo.getNumOp().getNumOrp() == null) {
                allResults=true;
            }
        }
        else{
            allResults=true;
        }
        
        try {
            con = ConnectionManager.getInstance().getConnection();            
            if(allResults){
                sql = "select * from num_serie";
                stm = con.prepareStatement(sql);
            }
            else{
               sql = "select * from num_serie where NUS_ORP_NUMERO = ? ";
               stm = con.prepareStatement(sql);
               stm.setInt(1, pojo.getNumOp().getNumOrp());
            }
            rs = stm.executeQuery();
            while (rs.next()) {
                NumeroSerie num = new NumeroSerie();
                OrdemProducaoDAO ordemProducaoDAO = new OrdemProducaoDAO();
                
                num.setNumSerie(rs.getString("NUS_NUMERO"));
                num.setNumOp(ordemProducaoDAO.readById(rs.getInt("NUS_ORP_NUMERO")));
                numeros.add(num);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return numeros ;
    }

    public NumeroSerie readById(String codigo) {
        Connection conn = null;
        PreparedStatement stm = null;
        NumeroSerie numeroSerie = null;
        ResultSet rs = null;
        String sql = "Select * from NUM_SERIE where NUS_NUMERO = ?";
        try {
            conn = ConnectionManager.getInstance().getConnection();
            stm = conn.prepareStatement(sql);
            stm.setString(1, codigo.toString());
            rs = stm.executeQuery();
            if (rs.next()){
                numeroSerie = new NumeroSerie();
                OrdemProducaoDAO ordemProducaoDAO = new OrdemProducaoDAO();
                numeroSerie.setNumSerie(rs.getString("NUS_NUMERO"));
                numeroSerie.setNumOp(ordemProducaoDAO.readById(rs.getInt("NUS_ORP_NUMERO")));
            }
          } catch (SQLException ex) {
                ex.printStackTrace();
          }
            finally{
        	try{
                    stm.close();
		}catch(SQLException ex){
                   ex.printStackTrace();
                }
                    try{
                        conn.close();
                    }catch(SQLException ex){
                        ex.printStackTrace();
                    }				
            }
            return numeroSerie;
    }

    public void delete(String codigo) {
        Connection conn = null;
        PreparedStatement stm = null;
        String sql = "delete * from NUM_SERIE where NUS_NUMERO = ?";
        try {
            conn = ConnectionManager.getInstance().getConnection();
            stm = conn.prepareStatement(sql);
            stm.setString(1, codigo.toString());
            stm.execute();
        } catch (SQLException ex) {
            ex.printStackTrace();
          }
            finally{
        	try{
                    stm.close();
		}catch(SQLException ex){
                    ex.printStackTrace();
                }
                    try{
                        conn.close();
                    }catch(SQLException ex){
                        ex.printStackTrace();
                    }				
            }
    }

    public NumeroSerie readById(Integer codigo) {
        throw new UnsupportedOperationException("Não é utilizado.");
    }

    public void delete(Integer codigo) {
        throw new UnsupportedOperationException("Nãoé utilizado.");
    }
}
